package cn.edu.scau.cmi.liujianxun.client.fxUI.transparentComposite;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import java.util.ArrayList;
import java.util.HashSet;

import cn.edu.scau.cmi.liujianxun.composite.transparent.*;
import javafx.event.ActionEvent;
import javafx.scene.control.TextArea;
import javafx.scene.control.ComboBox;

public class TransparentCompositeDemoController {
	@FXML
	private TextArea textArea;
	@FXML
	private Button btAddPerson;
	@FXML
	private Button btAddOrg;
	@FXML
	private TextField textField;
	@FXML
	private Button btAdd;
	@FXML
	private Button btShow;
	@FXML
	private Button btOpration;
	@FXML
	private ComboBox<String> cbBox2;
	@FXML
	private ComboBox<String> cbBox1;
	@FXML
	private Button btRemove;
	
	HashSet<Component> set = new HashSet<Component>();
	
	public void init() {
		Component p1 = new Person("海绵宝宝");
		Component o1 = new Organization("华南农业大学");
		Component o2 = new Organization("蟹堡王");
		o2.add(p1);
		set.add(o1);
		set.add(o2);
		set.add(p1);
		updateCbBox();
	}
	
	public Component getComponent(String name) {
		for(Component c : set) {
			if(name.equals(c.getName()))
			{
				return c;
			}
		}
		return null;
	}
	
	public void updateCbBox() {
		cbBox1.getItems().clear();
		cbBox2.getItems().clear();
		for(Component c : set) {
			cbBox2.getItems().add(c.getName());
		}
		for(Component c : set) {
			if(c instanceof Organization)
				cbBox1.getItems().add(c.getName());
		}
	}

	public void btAddPerson(ActionEvent event) {
		String name = textField.getText();
		for(Component c : set) {
			if(name.equals(c.getName()))
			{
				textArea.appendText("同名对象已存在，创建失败\n\n");
				return;
			}
		}
		Component p = new Person(name);
		set.add(p);
		textArea.appendText("创建成功\n\n");
		updateCbBox();
	}
	
	public void btAddOrg(ActionEvent event) {
		String name = textField.getText();
		for(Component c : set) {
			if(name.equals(c.getName()))
			{
				textArea.appendText("同名对象已存在，创建失败\n\n");
				return;
			}
		}
		Component p = new Organization(name);
		set.add(p);
		textArea.appendText("创建成功\n\n");
		updateCbBox();
	}
	
	public void btAdd(ActionEvent event) {
		String orgName = cbBox1.getValue();
		String compoName = cbBox2.getValue();
		if(!orgName.equals(compoName)) {
			Component org = getComponent(orgName);
			Component compo = getComponent(compoName);
			if(org.add(compo))
				textArea.appendText("添加成功！\n\n");
			else
				textArea.appendText("对象已存在，添加失败！\n\n");
		}
		else {
			textArea.appendText("不能对同一个对象操作！\n\n");
		}
	}
	
	public void btShow(ActionEvent event) {
		String orgName = cbBox1.getValue();
		Component org = getComponent(orgName);
		textArea.appendText(org.operation() + '\n');
		HashSet<Component> mySet = ((Organization)org).getSet();
		if(mySet.size() == 0) {
			textArea.appendText("我的组织还没有人！\n\n");
			return;
		}
		textArea.appendText("我的组织有以下这些成员：\n");
		for(Component c : mySet) {
			textArea.appendText(c.getName() + " ");
		}
		textArea.appendText("\n\n");
	}
	
	public void btOpration(ActionEvent event) {
		String name = cbBox2.getValue();
		Component com = getComponent(name);
		textArea.appendText(com.operation() + "\n\n");
	}
	
	public void btRemove(ActionEvent event) {
		String orgName = cbBox1.getValue();
		String compoName = cbBox2.getValue();
		if(!orgName.equals(compoName)) {
			Component org = getComponent(orgName);
			Component compo = getComponent(compoName);
			if(((Organization)org).remove(compo))
				textArea.appendText("删除成功！\n\n");
			else 
				textArea.appendText("不存在该成员！\n\n");
		}
		else {
			textArea.appendText("不能对同一个对象操作！\n\n");
		}
	}
}
